<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>主页</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">


</head>
<body>

  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script>
  $(function(){
	$.ajaxSetup({
		complete:function(XMLHttpRequest,textStatus){
			console.log(XMLHttpRequest,textStatus);
             if(XMLHttpRequest.status == 401){
                 //会话失效，跳转到登录页
                 window.location.replace("login.html");
             }else if(XMLHttpRequest.status == 303){
                 alert(XMLHttpRequest.responseText);
             }
          }
	});

  /*
		var email = sessionStorage.getItem("email");
		console.log(email);
		if(email!="" ){
			$("body").append("<h2>"+email+"，欢迎登录系统!</h2>");
		}
		*/
		var token = getCookie("token");
		$.ajax({
        url: "/api/user/info/"+token
        ,data: {}
		,xhrFields: {
			withCredentials: true // 这里设置了withCredentials
		}
        ,success: function(res){
			console.log(res);
			$("body").append("<h2>"+res+"，欢迎登录系统!</h2>");
        }
		});
		
  });
  
  
//js 获取cookie
function getCookie(cookie_name) {
        var allcookies = document.cookie;
		//索引长度，开始索引的位置
        var cookie_pos = allcookies.indexOf(cookie_name);

        // 如果找到了索引，就代表cookie存在,否则不存在
        if (cookie_pos != -1) {
            // 把cookie_pos放在值的开始，只要给值加1即可
            //计算取cookie值得开始索引，加的1为“=”
            cookie_pos = cookie_pos + cookie_name.length + 1; 
            //计算取cookie值得结束索引
            var cookie_end = allcookies.indexOf(";", cookie_pos);
            
            if (cookie_end == -1) {
                cookie_end = allcookies.length;

            }
            //得到想要的cookie的值
            var value = unescape(allcookies.substring(cookie_pos, cookie_end)); 
        }
        return value;
    }
  
  </script>
</body>
</html>